import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const home = createSlice({
    name: "home",
    initialState: {
        bannerList: []
    },
    reducers: {
        addBanner(state, action) {
            console.log("addBanner,",action.payload);
            state.bannerList.push(...action.payload);
        },
        removeBanner(state,action) {
            state.bannerList = state.bannerList.filter(item=>item.bannerid===action.payload);
        }
    }
});

// 对外暴露：actions
export const {addBanner,removeBanner} = home.actions;

// 对外暴露：异步操作
export const getBannerListAction=(dispatch)=>{
    axios({
        url:"http://121.89.205.189:3000/api/banner/list"
    })
    .then(res=>{
        if(res.data.code==="200"){
            dispatch(addBanner(res.data.data))
        }
    })
}


// 对外暴露reducer
export default home.reducer;

